import { BaseInjectHandler } from "../BaseInjectHandler";
import { EMessageC2C } from "../../../common/EMessageC2C";
import { HandleQuerySceneImg } from "./HandleQuerySceneImg";
import { TransPack } from "../../../server/framework/common/TransPack";
import { CommonOperateBack } from "../common/CommonOperateBack";
import QuaternionEx from "../../../LTGame/Utils/QuaternionEx";
import Vector3Ex from "../../../LTGame/Utils/Vector3Ex";

export class HandleSetSceneCameraPos extends BaseInjectHandler {

    msgId: number = EMessageC2C.SetSceneCameraPos;

    private _cameraHandle: HandleQuerySceneImg;

    private _cacheMove: Laya.Vector3;

    _OnInit() {
        this._cacheMove = new Laya.Vector3(0, 0, 0)
        this._cameraHandle = this.owner.GetHandleById(EMessageC2C.QuerySceneImg) as HandleQuerySceneImg;
    }

    _OnHandleMsg(msg: TransPack) {
        let posX = msg.data.x as number;
        let posZ = msg.data.z as number;

        let backData = new CommonOperateBack();
        if (!this._cameraHandle.isAbleToRender) {
            backData.code = -1;
            this.owner.SendTransOtherData(EMessageC2C.CommonOperate_Back, backData);
            return;
        }

        this._cacheMove.setValue(posX, 0, posZ);
        let wrapMove = QuaternionEx.Multiply(this._cameraHandle.workCamera.transform.rotation, this._cacheMove);
        this._cameraHandle.workCamera.transform.position = Vector3Ex.Add(this._cameraHandle.workCamera.transform.position, wrapMove);

        backData.code = 1;
        this.owner.SendTransOtherData(EMessageC2C.CommonOperate_Back, backData);
    }

}